package com.tywj.panda.user.order

import android.os.Bundle
import androidx.collection.ArrayMap
import com.chad.library.adapter.base.BaseQuickAdapter
import com.tywj.panda.customer.common.view.paging.BasePagingFragmentSearch
import com.tywj.panda.user.R
import com.tywj.panda.user.api.UserApi
import com.tywj.panda.user.entity.UserOrderEntity
import com.tywj.panda.user.route.UserRoute
import com.tywj.lib.net.retrofit.request.BaseRequest
import com.tywj.lib.route.Route
import com.tywj.lib.route.RouteParam

/**
 * @desc 我的订单记录页面
 * @author June Yang
 * @date 2020/5/13
 *
 */
@Route(path = UserRoute.ORDER_RECORD_DETAIL)
class UserOrderFragment : BasePagingFragmentSearch<UserOrderEntity, UserOrderEntity.DataBean>(
    UserOrderEntity.DataBean::class.java
) {

    companion object {
        const val PARAM_TYPE_ORDER_STATUS = "orderStatus"
    }

    private var orderStatus = ""

    override fun getRecyclerViewAdapter(): BaseQuickAdapter<UserOrderEntity.DataBean, *> {
        return UserOrderRecordAdapter()
    }

    override fun getRequest(): BaseRequest<*> = UserApi.listPriceOrder()

    override fun getRequestParams(): ArrayMap<String, String> {
        return UserApi.listBillingOrderParams(orderStatus)
    }

    //说明，由于服务器不能把返回的时间放到返回的数据最外层，交给前端来处理，在上拉更多时需要把返回的最后一条数据的时间交给服务器。
    override fun getRequestParamsLoadMore(): ArrayMap<String, String> {
        return UserApi.listBillingOrderParams(orderStatus, mAdapter.data.last().createDate)
    }

    override fun initVariable(savedInstanceState: Bundle?) {
        super.initVariable(savedInstanceState)

        orderStatus = RouteParam(this).getString(PARAM_TYPE_ORDER_STATUS)
    }

    override fun initListener() {
        super.initListener()
        mAdapter.setOnItemClickListener { _, _, position ->
            val bean = mAdapter.data[position]
            // 跳转至订单详情页
            UserRoute.toOrderDetails(bean.orderNo).navigation(this)
        }
    }
}